//
//  ComposeTextView.swift
//  Mircoblog-swift
//
//  Created by zz on 16/3/3.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit

class ComposeTextView: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        
        // 监听文本变化通知
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "textDidChanged",
            name: UITextViewTextDidChangeNotification,
            object: nil)
        
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 重写属性,便于外部设值
    override var font: UIFont?{
        didSet{
           placeholderLabel.font = font
        }
    }

    override var text: String!{
        didSet{
            placeholderLabel.hidden = hasText()
        }
    }
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    //MARK: - 通知方法
    @objc private func textDidChanged(){
    
        placeholderLabel.hidden = hasText()
        
        let maxlength: Int = 100
        
        let length = maxlength - text.characters.count
        
        tipLabel.text = length.description
        tipLabel.textColor = (length > 0) ? UIColor.lightGrayColor() : UIColor.redColor()
        
    }
    
    
    
    //MARK: - 私有控件
    private lazy var placeholderLabel: UILabel = {
    
        let textLabel = UILabel()
           textLabel.text = "分享微博"
           textLabel.font = UIFont.systemFontOfSize(18)
           textLabel.textColor = UIColor.redColor()
           textLabel.sizeToFit()
        return textLabel
    }()
    private lazy var tipLabel: UILabel = {
    
        let tipLab = UILabel()
        tipLab.text = "100"
        tipLab.font = UIFont.systemFontOfSize(12)
        tipLab.textColor = UIColor.redColor()
        tipLab.sizeToFit()
        return tipLab
    }()

}
extension ComposeTextView{

    private func setupUI(){

        backgroundColor = UIColor.yellowColor()
        font = UIFont.systemFontOfSize(18)
        
        // 始终允许垂直滚动
        alwaysBounceVertical = true
        
        // 拖动结束键盘编辑
        keyboardDismissMode = .OnDrag
        
        //设置contsize
        let sizeView: UIView = UIView()
        
        addSubview(sizeView)
        sizeView.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
            make.size.equalTo(self)
        }
        //添加占位字符
        addSubview(placeholderLabel)
        
         let margin: CGFloat = 5
        
        placeholderLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(8)
            make.top.equalTo(self).offset(margin)
            make.width.equalTo(self).offset(-2 * margin)
            
        }
        
        
        
        
        // 2> 添加长度提示标签
        addSubview(tipLabel)
        

        tipLabel.snp_makeConstraints { (make) in

            make.bottom.equalTo(self.snp_bottom).offset(-50)
            make.right.equalTo(self.snp_right).offset(-50)

        }
        
        
    }
}
